package com.example.service;

import com.example.entity.Account;
import com.example.entity.Likes;
import com.example.mapper.LikesMapper;
import com.example.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LikesService {

    @Resource
    LikesMapper likesMapper;

    public List<Integer> selectUserLikedBlogIds(Integer userId, String module) {
        return likesMapper.selectUserLikedBlogIds(userId, module);
    }

    public void set(Likes likes) {
        // 获取当前登录用户的信息
        Account currentUser = TokenUtils.getCurrentUser();
        // 将当前用户的 ID 设置到 Likes 对象中
        likes.setUserId(currentUser.getId());
        // 查询当前用户是否已经对该对象点赞
        Likes dblLikes = likesMapper.selectUserLikes(likes);
        // 如果查询结果为 null，表示用户尚未点赞
        if (dblLikes == null) {
            // 插入新的点赞记录
            likesMapper.insert(likes);
        } else {
            // 如果用户已经点赞，则删除该点赞记录，实现“取消点赞”功能
            likesMapper.deleteById(dblLikes.getId());
        }
    }


    /**
     *查询当前用户是否点过赞
     */
    public Likes selectUserLikes(Integer fid, String module){
        Account currentUser = TokenUtils.getCurrentUser();
        Likes likes = new Likes();
        likes.setUserId(currentUser.getId());
        likes.setFid(fid);
        likes.setModule(module);
        return likesMapper.selectUserLikes(likes);

    }


    public int selectByFidAndModule(Integer fid, String module) {
        return likesMapper.selectByFidAndModule(fid, module);
    }

}